package com.pipayshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.domain.ShopCollection;
import com.pipayshop.domain.vo.ShopCommodityCollectionVo;

import com.pipayshop.entity.vo.PageDataVO;
import com.pipayshop.mapper.OnlineStoreShopCollectionMapper;

import com.pipayshop.service.OnlineStoreShopCollectionService;
import com.pipayshop.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author xiaoqiu
 * @date 2024/9/20 15:03
 * @description
 */
@Service
public class OnlineStoreShopCollectionServiceImpl extends ServiceImpl<OnlineStoreShopCollectionMapper, ShopCollection> implements OnlineStoreShopCollectionService {

   @Autowired
   private OnlineStoreShopCollectionMapper shopCollectionMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "selectAllCollection", allEntries = true)
    public int AddShopCommodityToCollection(String userId, String commodityId) {
        // 先判断是否已经是关注过的，是就直接把状态改为0
        Long count = shopCollectionMapper.selectCount(new QueryWrapper<ShopCollection>()
                .eq("user_id", userId)
                .eq("commodity_id", commodityId));
        if (count == 0L){
            ShopCollection newShopCollection = new ShopCollection(null, StringUtil.generateShortId(), commodityId, userId, new Date(), new Date(), 0);
            return shopCollectionMapper.insert(newShopCollection);
        }
        return shopCollectionMapper.update(null, new UpdateWrapper<ShopCollection>()
                .eq("user_id", userId)
                .eq("commodity_id", commodityId)
                .set("status", 0));
    }

    @Override
    @CacheEvict(value = "selectAllCollection", allEntries = true)
    public int closeShopCommodityToCollection(String userId, String commodityId) {
        return shopCollectionMapper.update(null, new UpdateWrapper<ShopCollection>()
                .eq("user_id", userId)
                .eq("commodity_id", commodityId)
                .set("status", 1));
    }

    @Override
    public boolean isShopCommodityToCollection(String userId, String commodityId) {
        ShopCollection itemCollection = shopCollectionMapper.selectOne(new QueryWrapper<ShopCollection>()
                .eq("user_id", userId)
                .eq("commodity_id", commodityId)
                .eq("status", 0));
        return itemCollection != null;
    }

//    @Override
//    public List<ShopCommodityCollectionVo> selectAllCollection(String userId) {
//        List<ShopCommodityCollectionVo> shopCommodityCollectionVos = shopCollectionMapper.selectByUserId(userId);
//        return  shopCommodityCollectionVos;
//    }

    /**
     * 根据用户id查询 用户收藏的商品列表
     */
    @Override
    @Cacheable(value = "selectAllCollection", key = "'selectAllCollection'+#userId+#page+#limit")
    public PageDataVO selectAllCollection(Integer page, Integer limit, String userId) {
        Integer integer = shopCollectionMapper.selectAllCollectProductByUserId(userId);
        List<ShopCommodityCollectionVo> shopCommodityCollectionVos = shopCollectionMapper.selectCollectProductByUserId((page - 1) * limit, limit, userId);
        return  new PageDataVO(integer,shopCommodityCollectionVos);
    }



}
